//
//  MainViewController.m
//  ShopXinyi
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "MainViewController.h"
#import "HttpRequestManager.h"
#import "UIImageView+AFNetworking.h"
#import "MainScrollView.h"
#import "MainCell2.h"
#import "MainCell3.h"
#import "MainCell4.h"
#import "MainCell5.h"
#import "MainCell6.h"
#import "MainScrollModel.h"
#import "MainGoodsModel.h"



@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    NSMutableArray *_ScrollDataArray;
    NSMutableArray *_CategoryDataArray;
    NSMutableArray *_DiscountDataArray;
    NSMutableArray *_RecommendDataArray;
    NSMutableArray *_CatePictureDataArray;
    NSMutableArray *_LikeDataArray;
    float _discLine;
    float _likeLine;
    UITapGestureRecognizer *_tap;
}

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchGoods;

@end

@implementation MainViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _ScrollDataArray = [NSMutableArray new];
        _CategoryDataArray = [NSMutableArray new];
        _DiscountDataArray = [NSMutableArray new];
        _RecommendDataArray = [NSMutableArray new];
        _CatePictureDataArray = [NSMutableArray new];
        _LikeDataArray = [NSMutableArray new];
        _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestDataFromNet];

}



-(void)tap
{
    [self.view endEditing:YES];
}

-(void)requestDataFromNet
{
    [[HttpRequestManager shareInstance]getHomePageInfoSuccess:^(id responseObj) {
        
        NSArray *datasArray = responseObj;
        //滚动条数据
        NSDictionary *listDic = datasArray[0];
        NSDictionary *itemDic = listDic[@"adv_list"];
        NSArray *itemArray = itemDic[@"item"];
        for (NSDictionary *scrollDic in itemArray) {
            MainScrollModel *scrollModel = [MainScrollModel new];
            scrollModel.data = scrollDic[@"data"];
            scrollModel.imageURL = scrollDic[@"image"];
            [_ScrollDataArray addObject:scrollModel];
        }
        [_mainTableView reloadData];
        
        //热门分类数据
        NSDictionary *cateDic = datasArray[1];
        NSDictionary *home2Dic = cateDic[@"home2"];
        MainScrollModel *cateModel1 = [MainScrollModel new];
        cateModel1.data = home2Dic[@"rectangle1_data"];
        cateModel1.imageURL = home2Dic[@"rectangle1_image"];
        [_CategoryDataArray addObject:cateModel1];
        MainScrollModel *cateModel2 = [MainScrollModel new];
        cateModel2.data = home2Dic[@"rectangle1_data"];
        cateModel2.imageURL = home2Dic[@"rectangle2_image"];
        [_CategoryDataArray addObject:cateModel2];
        MainScrollModel *cateModel3 = [MainScrollModel new];
        cateModel3.data = home2Dic[@"square_data"]; //中文:洗发露
        cateModel3.imageURL = home2Dic[@"square_image"];
        [_CategoryDataArray addObject:cateModel3];
        
        NSDictionary *cateDic2 = datasArray[2];
        NSDictionary *home3Dic = cateDic2[@"home3"];
        NSArray *cateItemArray = home3Dic[@"item"];
        for (NSDictionary *dic in cateItemArray) {
            MainScrollModel *model = [MainScrollModel new];
            model.data = dic[@"data"];
            model.imageURL = dic[@"image"];
            [_CategoryDataArray addObject:model];
        }
        
        //限时折扣数据
        NSDictionary *discountDic = datasArray[3];
        NSDictionary *goodsDic =discountDic[@"goods"];
        NSArray *discItemArray = goodsDic[@"item"];
        for (NSDictionary *dic in discItemArray) {
            MainGoodsModel *model = [MainGoodsModel new];
            model.goodsID = dic[@"goods_id"];
            model.goodsImg = dic[@"goods_image"];
            model.goodsMKPrice = dic[@"goods_marketprice"];
            model.goodsName = dic[@"goods_name"];
            model.goodsPMPrice = dic[@"goods_promotion_price"];
            model.goodsPrice = dic[@"goods_price"];
            model.goodsStorage = dic[@"goods_storage"];
            [_DiscountDataArray addObject:model];
        }
        
        //精彩推荐
        NSDictionary *recommendDic = datasArray[4];
        NSDictionary *goodsDic2 =recommendDic[@"goods"];
        NSArray *discItemArray2 = goodsDic2[@"item"];
        for (NSDictionary *dic in discItemArray2) {
            MainGoodsModel *model = [MainGoodsModel new];
            model.goodsID = dic[@"goods_id"];
            model.goodsImg = dic[@"goods_image"];
            model.goodsMKPrice = dic[@"goods_marketprice"];
            model.goodsName = dic[@"goods_name"];
            model.goodsPMPrice = dic[@"goods_promotion_price"];
            model.goodsPrice = dic[@"goods_price"];
            model.goodsStorage = dic[@"goods_storage"];
            [_RecommendDataArray addObject:model];
        }
        
        //分类图片
        NSDictionary *pictureDic = datasArray[5];
        NSDictionary *PictureHomeDic = pictureDic[@"home3"];
        NSArray *pictureItemArray = PictureHomeDic[@"item"];
        for (NSDictionary *dic in pictureItemArray) {
            MainScrollModel *model = [MainScrollModel new];
            model.data = dic[@"data"];
            model.imageURL = dic[@"image"];
            [_CatePictureDataArray addObject:model];
        }
        
        //猜你喜欢
        NSDictionary *likeDic = datasArray[6];
        NSDictionary *likeGoodsDic =likeDic[@"goods"];
        NSArray *likeItemArray = likeGoodsDic[@"item"];
        for (NSDictionary *dic in likeItemArray) {
            MainGoodsModel *model = [MainGoodsModel new];
            model.goodsID = dic[@"goods_id"];
            model.goodsImg = dic[@"goods_image"];
            model.goodsMKPrice = dic[@"goods_marketprice"];
            model.goodsName = dic[@"goods_name"];
            model.goodsPMPrice = dic[@"goods_promotion_price"];
            model.goodsPrice = dic[@"goods_price"];
            model.goodsStorage = dic[@"goods_storage"];
            [_LikeDataArray addObject:model];
        }

        
        
    } failure:^(NSError *error) {
        NSLog(@"请求首页数据失败, error==%@",error);
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -UITableViewDelegate&UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        //滚动条视图
        MainScrollView *myView = [[MainScrollView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.width/2.5)];
        myView.backgroundColor = [UIColor clearColor];
        [myView makeScrollViewWithPictureNum:(int)_ScrollDataArray.count andPictureArray:_ScrollDataArray];
        self.mainTableView.tableHeaderView = myView;
    
        //固定分类
        if (indexPath.section == 1){
        MainCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell2"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainCell2" owner:self options:0][0];
        }
        return cell;
    //热门分类视图
    }else if (indexPath.section == 2){
        MainCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell3"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainCell3" owner:self options:0][0];
        }
        if (_CategoryDataArray.count > 0) {
            NSMutableArray *cateImgURLArray = [NSMutableArray new];
            for (MainScrollModel *model in _CategoryDataArray) {
                NSString *cateImgURL = model.imageURL;
                [cateImgURLArray addObject:cateImgURL];
            }
            [cell.chsg setImageWithURL:[NSURL URLWithString:cateImgURLArray[0]]];
            [cell.mjs setImageWithURL:[NSURL URLWithString:cateImgURLArray[1]]];
            [cell.cjthr setImageWithURL:[NSURL URLWithString:cateImgURLArray[2]]];
            [cell.lymm setImageWithURL:[NSURL URLWithString:cateImgURLArray[3]]];
            [cell.yybj setImageWithURL:[NSURL URLWithString:cateImgURLArray[4]]];
        }
        
        return cell;
    //限时折扣
    }else if (indexPath.section == 3){
        MainCell4 *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell4"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainCell4" owner:self options:0][0];
        }
        cell.dataArray = [NSArray arrayWithArray:_DiscountDataArray];
        cell.rootVC = self;
        [cell.myCollectionView reloadData];
        return cell;
    //精彩推荐
    }else if(indexPath.section == 4){
        MainCell5 *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell5"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainCell5" owner:self options:0][0];
        }
        cell.dataArray = [NSArray arrayWithArray:_RecommendDataArray];
        [cell.myCollectionView reloadData];
        return cell;
    //分类图片
    }else if(indexPath.section == 5){
        MainCell6 *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell6"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainCell6" owner:self options:0][0];
        }
        cell.dataArray = [NSArray arrayWithArray:_CatePictureDataArray];
        [cell.myCollectionView reloadData];
        return cell;
    //猜你喜欢
    }else{
        MainCell4 *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell4"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainCell4" owner:self options:0][0];
        }
        cell.dataArray = [NSArray arrayWithArray:_LikeDataArray];
        cell.rootVC = self;
        [cell.myCollectionView reloadData];
        return cell;
    }
}


//每段 cell 高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 1:
            return self.view.frame.size.width/4.8;
            break;
        case 2:
            return self.view.frame.size.width*272/320.0;
            break;
        case 3:
            _discLine = _DiscountDataArray.count/3;
            if (_DiscountDataArray.count%3 != 0) {
                _discLine += 1;
            }
            return self.view.frame.size.width*0.48*_discLine;
            break;
        case 4:
            return self.view.frame.size.width*0.5;
            break;
        case 5:
            return self.view.frame.size.width*0.8;
            break;
        case 6:
            _likeLine = _LikeDataArray.count/3;
            if (_LikeDataArray.count/3 != 0) {
                _likeLine += 1;
            }
            return self.view.frame.size.width*0.48*_likeLine;
            break;
        default:
            break;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section < 3 || section == 5) {
        return 5;
    }else
        return 25;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section < 3 || section == 5) {
        return nil;
    }else{
        UIView *head = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 25)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 20)];
        label.textColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:14];
        switch (section) {
            case 3:
                label.text = @"限时折扣";
                break;
            case 4:
                label.text = @"精彩推荐";
                break;
            case 6:
                label.text = @"猜你喜欢";
                break;
            default:
                break;
        }
        [head addSubview:label];
        return head;
    }
    
}


//解决手势冲突
#pragma mark -UISearchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    self.view.userInteractionEnabled = YES;

    [self.view addGestureRecognizer:_tap];
    return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [self.view removeGestureRecognizer:_tap];
    return YES;
}

@end
